import axios from 'axios'

export const request = {
    axiosGet (url, params, baseUrl) {
        return requestInstance({
            baseURL,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
            },
            method: 'GET',
            url,
            params,
        })
    },
    axiosPost (url, data, params, baseUrl) {
        return requestInstance({
            baseURL,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
            },
            method: 'POST',
            url,
            params,
            data,
        })
    },
}

export function goto(path) {
    this.$router.push({path: path, query: {time: new Date().valueOf().toString()}})
}

export function setCookie(cname, value, time = 100) {
    if (cname && time) {
        let exdate = new Date();
        exdate.setTime(exdate.valueOf() + time * 24 * 60 * 60 * 1000);
        document.cookie = cname + "=" + encodeURI(value) + ";expires=" + exdate.toGMTString();
    }
}

export function getCookie(name) {
    let cookie = ''
    let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    arr = document.cookie.match(reg)
    if (arr) {
        cookie = decodeURI(arr[2]);
        return cookie
    }
    return null
}

export function delCookie(name) {
    if (!name) { return }
    let exp = new Date();
    exp.setTime(exp.valueOf() - 1);
    const cval = getCookie(name)
    if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}
